/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MediumActionDialog.java
 *
 * Created on 9.5.2011, 12:59:23
 */
package GUI;

import Model.*;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Point;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author pyty
 */
public class MediumActionDialog extends javax.swing.JDialog {

    private Frame parent = null;
    private Medium medium = null;
    
    //private ResourceBundle texts = java.util.ResourceBundle.getBundle("i18n/texts");
    
    /** Creates new form MediumActionDialog */
    public MediumActionDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        this.parent = parent;

        initComponents();
    }
    
    public MediumActionDialog(java.awt.Frame parent, boolean modal, Medium medium) {
        super(parent, modal);
        this.parent = parent;

        this.medium = medium;
        
        initComponents();
    }


    public void disableButton() {
        BorrowManagerImpl borrowManager = new BorrowManagerImpl();
        try {
            borrowManager.setDataSource(MainFrame.prepareDataSource());
        } catch (SQLException ex) {
            Logger.getLogger(MediumActionDialog.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        Client client = borrowManager.findClientWithMedium(medium);
        
        if (client == null) {
            jButton3.setEnabled(false);
            jButton2.setEnabled(true);
        } else {
            jButton3.setEnabled(true);
            jButton2.setEnabled(false);
            jTextField3.setText(client.getName());
        }
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jComboBox1 = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jTextField3 = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jButton3 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("i18n/texts"); // NOI18N
        setTitle(bundle.getString("Add/EditMedium")); // NOI18N
        setBounds(new java.awt.Rectangle(400, 300, 0, 0));
        setResizable(false);

        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(RBUtils.getLocalizedMediumTypes()));
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        jTextField3.setEditable(false);
        jTextField3.setText(bundle.getString("MediumActionDialog.jTextField3.text")); // NOI18N
        jTextField3.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));

        jButton2.setText(bundle.getString("MediumActionDialog.jButton2.text")); // NOI18N
        jButton2.setEnabled(false);
        jButton2.setMaximumSize(new java.awt.Dimension(59, 23));
        jButton2.setMinimumSize(new java.awt.Dimension(59, 23));
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton1.setText(bundle.getString("MediumActionDialog.jButton1.text")); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel1.setText(bundle.getString("MediumActionDialog.jLabel1.text_1")); // NOI18N

        jLabel2.setText(bundle.getString("MediumActionDialog.jLabel2.text")); // NOI18N

        jLabel3.setText(bundle.getString("MediumActionDialog.jLabel3.text")); // NOI18N

        jLabel4.setText(bundle.getString("MediumActionDialog.jLabel4.text")); // NOI18N

        jLabel5.setText(bundle.getString("MediumActionDialog.jLabel5.text")); // NOI18N

        jButton3.setText(bundle.getString("MediumActionDialog.jButton3.text")); // NOI18N
        jButton3.setEnabled(false);
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, 121, Short.MAX_VALUE))
                    .addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 256, Short.MAX_VALUE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 256, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 256, Short.MAX_VALUE)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField3, javax.swing.GroupLayout.DEFAULT_SIZE, 256, Short.MAX_VALUE))
                .addGap(22, 22, 22))
            .addGroup(layout.createSequentialGroup()
                .addGap(93, 93, 93)
                .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 280, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5)))
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton3))
                .addGap(22, 22, 22)
                .addComponent(jButton1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_jComboBox1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        ClientChooserDialog dialog = new ClientChooserDialog(parent, true);
        dialog.setMedium(medium);
        dialog.setParent(this);
        dialog.setVisible(true);
}//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if("".equals(jTextField1.getText())) {
            JOptionPane.showMessageDialog(this, RBUtils.getBoundle("error-notFilledMediumName"));
        } else {
            if(this.medium == null) {
                Medium medium = new Medium();
                
                medium.setName(jTextField1.getText());
                medium.setAuthor(jTextField2.getText());
                medium.setType((String) MediumTypes.STRINGS[jComboBox1.getSelectedIndex()]);
                medium.setDescription(jTextArea1.getText());
                
                MainFrame mediumManagerFrame = (MainFrame) parent;
                mediumManagerFrame.addMedium(medium);
                
                this.setVisible(false);
            } else {
                medium.setName(jTextField1.getText());
                medium.setAuthor(jTextField2.getText());
                medium.setType((String) MediumTypes.STRINGS[jComboBox1.getSelectedIndex()]);
                medium.setDescription(jTextArea1.getText());
                
                MainFrame mediumManagerFrame = (MainFrame) parent;
                mediumManagerFrame.editMedium(medium);
                
                this.setVisible(false);
            }
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField1ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        BorrowManagerImpl borrowManager = new BorrowManagerImpl();
        try {
            borrowManager.setDataSource(MainFrame.prepareDataSource());
        } catch (SQLException ex) {
            Logger.getLogger(MediumActionDialog.class.getName()).log(Level.SEVERE, null, ex);
        }
        borrowManager.returnBorrow(medium);
        disableButton();
        jTextField3.setText("");
    }//GEN-LAST:event_jButton3ActionPerformed

    
    public void centerParent () {
        int x;
        int y;

        // Find out our parent 
        Container myParent = getParent();
        Point topLeft = myParent.getLocationOnScreen();
        Dimension parentSize = myParent.getSize();

        Dimension mySize = getSize();

        if (parentSize.width > mySize.width) 
          x = ((parentSize.width - mySize.width)/2) + topLeft.x;
        else 
          x = topLeft.x;

        if (parentSize.height > mySize.height) 
          y = ((parentSize.height - mySize.height)/2) + topLeft.y;
        else 
          y = topLeft.y;

        setLocation (x, y);
        //this.setVsible(true);
        requestFocus();
    }  
    
    public void setMedium(Medium medium) {
        this.medium = medium;
        jTextField1.setText(medium.getName());
        jTextField2.setText(medium.getAuthor());
        //jTextField3.setText(medium.getType());
        
        int index = 0;
        if (medium.getType().equals("CD")) {index = 1;}
        if (medium.getType().equals("Book")) {index = 2;}
                    
        jComboBox1.setSelectedIndex(index);
        jTextArea1.setText(medium.getDescription());
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                MediumActionDialog dialog = new MediumActionDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.centerParent();
                //if (dialog.medium == null) {
                //    dialog.setTitle(RBUtils.getBoundle("addMedium"));
                //}
                dialog.setTitle("BLA BLA");
                
                
                dialog.setVisible(true);
                
                //dialog.setLocationRelativeTo(parent.JButton5);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    // End of variables declaration//GEN-END:variables
}
